<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <jsp:include page="../common/common.jsp"/>
    <title>角色权限配置</title>
    <link rel="stylesheet" type="text/css"	href="${webroot}/static/plugins/zTree/easyui.css">
	<link rel="stylesheet" type="text/css"	href="${webroot}/static/plugins/zTree/icon.css">
	<link rel="stylesheet"	href="${webroot}/static/css/bootstrapValidator.css">
	<link rel="stylesheet"	href="${webroot}/static/plugins/inputFile/css/ssi-uploader.css">
	<link rel="stylesheet" href="${webroot}/static/css/reset.css">
	<link rel="stylesheet" type="text/css"	href="${webroot}/static/css/merchantConfig.css">
</head>
<%  
	String roleName = request.getParameter("roleName");
	roleName = new String(roleName.getBytes("iso-8859-1"),"UTF-8");
	System.out.println(roleName);
 %>  
<body>
	<p>勾选的权限将分配给当前操作角色：<span style="padding-left: 10px"></span><font color="red" size="4px"><%=roleName %></font></p>
	<article id="" class="searchsForm clearfix form-horizontal">
		<form id="" method="post" class="form-horizontal">
			<div class="form-group clearfix">
				<div class="col-lg-6 col-md-6"></div>
				<div class="col-lg-6 col-md-6">
					<a id="subButton" class="btn btn-primary w-50 fr" style="margin-left: 10px; margin-right: 10px;" onclick="updateRolePermission();">保存</a> 
					<a style="color: #fff;" href="javascript:history.go(-1);">
						<input type="text" id="backButton" value="返回上一级" class="btn btn-primary w-50 fr" style="margin-left: 10px; width: 98px !important; margin-right: 10px;">
					</a>
				</div>
			</div>
		</form>
	</article>
	<input type="hidden" id="roleId" value="${param.roleId }">
	<!--第二类-->
	<section style="margin-top: 20px; position: relative;">
		<div class="easyui-panel" style="padding: 5px; padding-bottom: 100px">
			<ul id="tt" class="easyui-tree">
			</ul>
		</div>
	</section>
	<script type="text/javascript"	src="${webroot}/static/plugins/zTree/jquery.easyui.min.js"></script>
	<script src="${webroot}/static/js/bootstrapValidator.js"></script>
	<script src="${webroot}/static/plugins/inputFile/js/ssi-uploader.js"></script>
	
	<script type="text/javascript">
	$(document).ready(function(){
		$('#tt').tree({
			url: '${webroot}/admin/system/queryRolePermission?roleId=${param.roleId }',
			method: 'get',
			checkbox:true,
			cascadeCheck:false,//取消级联检查,否则去掉子节点的时候，父节点也会被取消
			onCheck:function(node,checked){
				checkedChildNode(node,checked);
			},
			onLoadSuccess:function(){
				$('#tt').tree('expandAll');
			}
		});		
	});
	//递归实现子节点的选中和不选中
	function checkedChildNode(node,checked){
		var childList = $("#tt").tree('getChildren',node.target);//可以获取到所有的子节点
		if(childList.length != 0){
			var funcName = "";//函数名称
			if(true == checked){
				funcName = 'check';
			}else{
				funcName = 'uncheck';
			}
			for(var i = 0; i < childList.length;i++ ){
				$("#tt").tree(funcName,childList[i].target);
			}
		}
	}
	function updateRolePermission(){
		$.confirm({
		      title: '确定保存角色的新权限？',
		      columnClass:'col-md-5 col-md-offset-3',
		      type:'green',
		      icon:'glyphicon glyphicon-question-sign',
		      buttons: {
		        ok: {
		            text: '确认',
		            btnClass: 'btn-primary',
		            action: function(){
		            	var nodes = $('#tt').tree('getChecked');
		        		var ids = new Array();
		        		for(var i = 0;i < nodes.length;i++){
		        			ids.push(nodes[i].id);
		        		}
		        		if(ids != null){
		        			$.ajax({
		        	            type:'post',
		        	            url:webroot+'/admin/system/updateRolePermission',
		        	            data:{"ids":ids,roleId:$("#roleId").val()},
		        	            async:false,
		        	            dataType:'json',
		        	            success:function(data){
		        	            	checkResponseData(data,"1");
		        	            },
		        	            error:function(){
		        	            	 $.alert({title:"错误", content:"请求修改角色权限接口错误，请查找原因！"});
		        	            }
		        	        });
		        		}
		            }
		        },
	        cancel: {
	            text: '取消',
	            btnClass: 'btn-primary',
	            action: function(){
	            }
	        },
	      }
	     });
	}
	</script>
</body>
</html>